package com.kyxx.topics.activity;

import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.bm.library.PhotoView;
import com.kyxx.topics.BaseActivity;
import com.kyxx.topics.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ViewPagerActivity extends BaseActivity implements ViewPager.OnPageChangeListener{

    @Bind(R.id.pager)
    ViewPager mPager;
    @Bind(R.id.AddLayout)
    LinearLayout AddLayout;
    @Bind(R.id.moveView)
    View moveView;
    @Bind(R.id.addrelative)
    RelativeLayout addrelative;
    private String[] bitmaps;
    private ImageLoader imageLoader = ImageLoader.getInstance();
    private DisplayImageOptions options;
    private int index = 0;
    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_view_pager);
        ButterKnife.bind(this);
        bitmaps = getIntent().getStringArrayExtra("bitmap");
        index = getIntent().getIntExtra("index", 0);
        options = new DisplayImageOptions.Builder()
//				// 设置图片在下载期间显示的图片
                .showImageOnLoading(R.mipmap.image_default)
//				// 设置图片Uri为空或是错误的时候显示的图片
                .showImageForEmptyUri(R.mipmap.image_default)
//				// 设置图片加载/解码过程中错误时候显示的图片
                .showImageOnFail(R.mipmap.image_default)
                .cacheInMemory(true)
                // 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)
                // 设置下载的图片是否缓存在SD卡中
                .bitmapConfig(Bitmap.Config.RGB_565)// 设置图片的解码类型
//                .displayer(new RoundedBitmapDisplayer(9999))
                .build();
    }

    @Override
    protected void findViewById() {

    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void processLogic() {
        AddInstructionsView();
        if(bitmaps.length == 1){
            addrelative.setVisibility(View.GONE);
        }
        mPager.setOnPageChangeListener(this);
        mPager.setPageMargin((int) (getResources().getDisplayMetrics().density * 15));
        mPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return bitmaps.length;
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                PhotoView view = new PhotoView(ViewPagerActivity.this);
                view.enable();
                view.setScaleType(ImageView.ScaleType.FIT_CENTER);
                imageLoader.displayImage(bitmaps[position], view, options);
                container.addView(view);
                return view;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }
        });
        mPager.setCurrentItem(index);
        moveView.setX(dip2px(10) * index);
    }

    private void AddInstructionsView() {
        for (int i = 0; i < bitmaps.length; i++) {
            View addview = new View(ViewPagerActivity.this);
            addview.setBackgroundColor(0xffffffff);
            LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(dip2px(5), dip2px(5));
            addview.setLayoutParams(p);
            p.setMargins(dip2px(5), 0, 0, 0);
            AddLayout.addView(addview);
        }
    }
    public int dip2px(float dpValue) {
        final float scale = getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        moveView.setX(dip2px(5) + dip2px(10) * position + dip2px(10) * positionOffset);
    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
